package com.cnc.tag;

/**
 * Created by Longtop
 * User: Dubf
 * Date: 2004-11-11
 * Time: 17:39:21
 */

import com.cnc.bas.util.StringUtil;
import com.cnc.ssm.ISsmService;
import com.cnc.ssm.UserInfo;

import org.apache.struts.taglib.html.Constants;
import org.apache.struts.taglib.html.SelectTag;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * @author dubf
 * @version 1.0 2004-5-4 15:41:22
 */
public class ListUserTag extends LtcmTag {
    protected boolean disabled = false;
    private boolean condition = false;
    private boolean listall = false;
    private boolean showcode = true;
    private String officecode;

    public boolean isListall() {
        return listall;
    }

    public void setListall(boolean listall) {
        this.listall = listall;
    }

    public ListUserTag() {
        super();
    }

    public boolean isCondition() {
        return condition;
    }

    public void setCondition(boolean condition) {
        this.condition = condition;
    }

    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public boolean isShowcode() {
        return showcode;
    }

    public void setShowcode(boolean showcode) {
        this.showcode = showcode;
    }

    public String getOfficecode() {
        return officecode;
    }

    public void setOfficecode(String officecode) {
        this.officecode = officecode;
    }

    public int doEndTag() throws JspException {
        ISsmService ssmService = (ISsmService) this.getBean(com.cnc.bas.util.Constants.BEAN_NAME_SSM);
        if (this.listall)
            this.officecode = null;
        else if (this.officecode == null) {
            HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
            UserInfo oper = (UserInfo) request.getSession().getAttribute(com.cnc.bas.util.Constants.USER_KEY);
//          liuwei修改过
            //this.officecode = oper.getInstCode();
        }
        HashMap hs = new HashMap();
        hs.put("instCode", this.officecode);
        List list = ssmService.listOperator(hs);
        Iterator iter = list.iterator();
        StringBuffer results = new StringBuffer();
        if (condition) {
            results.append("<option value=''");
            if (disabled) {
                results.append(" disabled=\"disabled\"");
            }
            results.append(">");

            results.append("</option>");
        }
        while (iter.hasNext()) {
            HashMap custma = (HashMap) iter.next();
            String usercode = (String.valueOf(custma.get("OPER_CODE"))).trim();
            String username = (String.valueOf(custma.get("OPER_NAME"))).trim();
            results.append("<option value=\"" + usercode + "\"");
            if (disabled) {
                results.append(" disabled=\"disabled\"");
            }

            if (this.selectTag() != null) {
                if (this.selectTag().isMatched(usercode)) {
                    results.append(" selected=\"selected\"");
                }
            }

            results.append(">");
            if (this.showcode)
                results.append(StringUtil.toFixLength(usercode, 20) + "  |  ");
            results.append(username);
            results.append("</option>");
        }
        ResponseUtils.write(pageContext, results.toString());
        return (EVAL_PAGE);
    }

    private SelectTag selectTag() throws JspException {
        SelectTag selectTag =
                (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);

        if (selectTag == null) {
            JspException e =
                    new JspException("error : no select element");

            RequestUtils.saveException(pageContext, e);
            throw e;
        }

        return selectTag;
    }
}